package com.stlm2.dems.service;

import com.stlm2.dems.dao.DdmGoodsDao;
import com.stlm2.dems.dao.UserDao;
import com.stlm2.dems.dao.ValueAddedServicesDao;
import com.stlm2.dems.entity.DdmGoods;
import com.stlm2.dems.entity.User;
import com.stlm2.dems.entity.ValueAddedServices;
import com.stlm2.util.Memcached;
import com.stlm2.util.codecontent.CodeContent;
import org.apache.commons.lang.math.NumberUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;

import com.stlm2.dems.entity.GoodsValueAddedServices;
import com.stlm2.dems.dao.GoodsValueAddedServicesDao;
import com.stlm2.core.base.BaseDao;
import com.stlm2.core.base.impl.BaseServiceImpl;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service("goodsValueAddedServicesService")
@Transactional(rollbackForClassName={"java.lang.Exception"}, propagation = Propagation.REQUIRED)
public class GoodsValueAddedServicesService extends BaseServiceImpl<GoodsValueAddedServices> {

	@Resource
	private GoodsValueAddedServicesDao goodsValueAddedServicesDao;
	@Resource
	private UserDao userDao;
	@Resource
	private Memcached memcached;
	@Resource
	private DdmGoodsDao ddmGoodsDao;
	@Resource
	private ValueAddedServicesDao valueAddedServicesDao;

	@Override
	protected BaseDao <GoodsValueAddedServices> getDao() {
		return goodsValueAddedServicesDao;
	}
	/*
	*zlq
	* 商品添加增值服务
	* token:
	* goodsId:商品ID
	* valueAddedServicesId:增值服务ID
	* warrantyPeriod：总有效期(单位 / 月)
	 */
	public String add(ModelMap model, String token,String goodsId,String valueAddedServicesId,String warrantyPeriod){
		if (StringUtils.isEmpty(token)) {
			return CodeContent.getModelByCode(-1, "", model);
		}
		User loginUser = userDao.getByToken(token);
		if (loginUser == null) {
			return CodeContent.getModelByCode(-1, "", model);
		}
		if (!NumberUtils.isDigits(goodsId)) {
			return CodeContent.getModelByCode(0, "商品ID不能为空", model);
		}
		if (!NumberUtils.isDigits(warrantyPeriod)) {
			return CodeContent.getModelByCode(0, "有效期不能为空", model);
		}
		if (StringUtils.isEmpty(valueAddedServicesId)) {
			return CodeContent.getModelByCode(0, "增值服务ID不能为空", model);
		}
		DdmGoods ddmGoods=ddmGoodsDao.getById(Integer.parseInt(goodsId));
		if(ddmGoods==null){
			return CodeContent.getModelByCode(0, "商品ID不存在", model);
		}
		ValueAddedServices valueAddedServices=valueAddedServicesDao.getById(Integer.parseInt(valueAddedServicesId));
		if(valueAddedServices==null){
			return CodeContent.getModelByCode(0, "增值服务ID不存在", model);
		}
		int goodsWarrantyPeriod=ddmGoods.getWarrantyPeriod()!=null?ddmGoods.getWarrantyPeriod():0;
		int servicesWarrantyPeriod=valueAddedServices.getWarrantyPeriod();
		if(Integer.parseInt(warrantyPeriod)<servicesWarrantyPeriod){
			return CodeContent.getModelByCode(0, "有效期不能小于该增值服务的有效期", model);
		}
		if(Integer.parseInt(warrantyPeriod)>goodsWarrantyPeriod+servicesWarrantyPeriod){
			return CodeContent.getModelByCode(0, "有效期不能大于该增值服务和商品保修期之和", model);
		}

		GoodsValueAddedServices goodsValueAddedServices =new GoodsValueAddedServices();
		goodsValueAddedServices.setGoodsId(ddmGoods.getId());
		goodsValueAddedServices.setValueAddedServicesId(valueAddedServices.getId());
		goodsValueAddedServices.setWarrantyPeriod(Integer.parseInt(warrantyPeriod));
		List <GoodsValueAddedServices> goodsValueAddedServicesList1=goodsValueAddedServicesDao.queryPageView(-1,-1,goodsValueAddedServices).getRecords();
		if(goodsValueAddedServicesList1.size()!=0){
			return CodeContent.getModelByCode(0, "商品已经添加过:" + valueAddedServices.getName(), model);
		}
		//防止重复提交
		Object o = memcached.get(ddmGoods.getId()+"addGoodsValueAddedServices");
		if(o!=null){
			return CodeContent.getModelByCode(0, "操作频繁", model);
		}else {
			memcached.set(ddmGoods.getId()+"addGoodsValueAddedServices",1,"1");
		}
		goodsValueAddedServicesDao.add(goodsValueAddedServices);

		return CodeContent.getModelByCode(1, "", model);
	}
	/*
	*商品删除增值服务
	* zlq
	* token:
	* id: 记录ID
	 */
	public String delete(ModelMap model, String token,String id){
		if (StringUtils.isEmpty(token)) {
			return CodeContent.getModelByCode(-1, "", model);
		}
		User loginUser = userDao.getByToken(token);
		if (loginUser == null) {
			return CodeContent.getModelByCode(-1, "", model);
		}
		if (!NumberUtils.isDigits(id)) {
			return CodeContent.getModelByCode(0, "ID不能为空", model);
		}
		GoodsValueAddedServices goodsValueAddedServices=goodsValueAddedServicesDao.getById(Integer.parseInt(id));
		if(goodsValueAddedServices==null){
			return CodeContent.getModelByCode(0, "ID不存在", model);
		}
		goodsValueAddedServicesDao.delete(goodsValueAddedServices.getId());
		return CodeContent.getModelByCode(1, "", model);
	}

	/*
*zlq
* 商品查找叮当猫增值服务列表
* token:
* goodsId:商品ID
*/
	public String getlist(ModelMap model, String token,String goodsId) {
		if (StringUtils.isEmpty(token)) {
			return CodeContent.getModelByCode(-1, "", model);
		}
		User loginUser = userDao.getByToken(token);
		if (loginUser == null) {
			return CodeContent.getModelByCode(-1, "", model);
		}
		if (StringUtils.isEmpty(goodsId)) {
			return CodeContent.getModelByCode(0, "商品ID不能为空", model);
		}
		DdmGoods ddmGoods=ddmGoodsDao.getById(Integer.parseInt(goodsId));
		if(ddmGoods!=null){
			List<GoodsValueAddedServices> goodsValueAddedServicesList=goodsValueAddedServicesDao.getByGoodsId(ddmGoods.getId());
			return CodeContent.getModelByCode(1, "", convertList(goodsValueAddedServicesList));
		}

		return CodeContent.getModelByCode(1, "", model);
	}

	public List<ModelMap> convertList(List<GoodsValueAddedServices> goodsValueAddedServicesList) {
		List<ModelMap> list = new ArrayList<ModelMap>();
		if (goodsValueAddedServicesList != null && goodsValueAddedServicesList.size() > 0) {
			for (GoodsValueAddedServices goodsValueAddedServices : goodsValueAddedServicesList) {
				ModelMap map = buildModelList(goodsValueAddedServices);
				list.add(map);
			}
		}
		return list;
	}
	public ModelMap buildModelList(GoodsValueAddedServices goodsValueAddedServices ) {
		ModelMap model = new ModelMap();
		model.put("id",goodsValueAddedServices.getId());
		model.put("warrantyPeriod",goodsValueAddedServices.getWarrantyPeriod()+ "个月");
		model.put("name",goodsValueAddedServices.getValueAddedServicesList().size()!=0?goodsValueAddedServices.getValueAddedServicesList().get(0).getName():"");
		model.put("price",goodsValueAddedServices.getValueAddedServicesList().size()!=0?goodsValueAddedServices.getValueAddedServicesList().get(0).getPrice():"");

		return  model;
	}

	/*
	* 判断增值服务是否属于某个商品
	* sfy
	* 2017年11月7日10:13:59
	* */
	public Boolean ifBelongGood(int goodId,String addServiceId){
		String[] args = addServiceId.split(",");
		for(int i=0;i<args.length;i++){
			GoodsValueAddedServices goodsValueAddedServices  = goodsValueAddedServicesDao.getByGoodsIdAndServiceId(goodId,Integer.valueOf(args[i]));
			  if(goodsValueAddedServices==null){
                   return false;
			  }else {

				  return true;
			  }

		}

		return  null;
	}


}